package com.neteast.androidclient.contact;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import org.json.JSONObject;
import com.neteast.androidclient.contact.domain.Contact;
import com.neteast.androidclient.contact.domain.ContactData;
import com.neteast.androidclient.contact.domain.ContactRowManager;
import com.neteast.androidclient.contact.util.ContactsUtils;
import com.neteast.androidclient.contact.util.JsonUtil;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.Contacts;
import android.test.AndroidTestCase;
import android.util.Log;

public class Test extends AndroidTestCase {
	
	public void testContact() throws Throwable {
		Contact contact=getContact();
		
		Contact cloneContact = contact.clone();
		ContactData data=new ContactData();
		data.id=2;
		data.label="公司";
		data.content="01098978876";
		data.modified=Constant.CONTACT_NEW;
		contact.phones.add(data);
		
		contact.groups="2,3";
		contact.phones.get(0).id=4;
		Log.i("test", "contact="+contact.toString());
		Log.i("test", "cloneContact="+cloneContact.toString());
		
		JSONObject jsonObject = ContactRowManager.isContactChanged(cloneContact,contact);
		if (jsonObject!=null) {
			Log.i("test", jsonObject.toString());
		}
	}
	public static Contact getContact() {
		Contact contact=new Contact();
		contact.ownId=1;
		contact.contactId=2;
		contact.displayName="张三";
		contact.nickName="小三三";
		contact.organization="东方网信";
		contact.job="首席大门执行官";
		contact.photo="/mnt/sdcard/photo/1.png";
		contact.hasPhoto=true;
		contact.remark="看门的";
		contact.state="在东方网信看大门中";
		contact.groups="家人,朋友";
		
		ArrayList<ContactData> phones=new ArrayList<ContactData>();
		
		ContactData data=new ContactData();
		data.id=0;
		data.label="手机";
		data.content="12345677543";
		data.modified=Constant.CONTACT_NEW;
		phones.add(data);
		
		data=new ContactData();
		data.id=1;
		data.label="住宅";
		data.content="010-87686321";
		data.modified=Constant.CONTACT_NEW;
		data.isReadOnly=true;
		phones.add(data);
		
		data=new ContactData();
		data.id=2;
		data.label="公司";
		data.content="01098978876";
		data.modified=Constant.CONTACT_NEW;
		data.isReadOnly=true;
		phones.add(data);
		
		data=new ContactData();
		data.id=3;
		data.label="其他";
		data.content="010-88167543";
		data.modified=Constant.CONTACT_NEW;
		phones.add(data);
		
		Collections.sort(phones);
		contact.phones.addAll(phones);
		
		Log.i("test", contact.phones.toString());
		return contact;
	}
	
	public void testSysContacts() throws Throwable {
		Cursor cursor = ContactsUtils.getAllContacts(getContext());
		StringBuilder sb=new StringBuilder();
		if (cursor.moveToFirst()) {
			do {
				String _id = cursor.getString(0);
				Contact contact = ContactsUtils.populateContact(getContext(), _id);
				Log.i("test", contact.toString());
			} while (cursor.moveToNext());
			cursor.close();
		}
	}
	public void testTranslate() throws Throwable {
		Contact contact=new Contact();
		JSONObject json = JsonUtil.translateContactToJson(contact);
		Log.i("test", json.toString());
	}
	
	public void testDao() throws Throwable{
		String displayName = ContactsUtils.getDisplayName(getContext(), "64");
		Log.i("test", "displayName="+displayName);
	}
	
	public void testQucik() throws Throwable {
		Context context = getContext();
		Cursor cursor = context.getContentResolver().query(
				Contacts.CONTENT_URI, 
				new String[]{"sort_key"},
				null, 
				null, 
				null);
		if (cursor.moveToFirst()) {
			do {
				Log.i("test","sort_key="+cursor.getString(0).substring(0, 1));
			} while (cursor.moveToNext());
			cursor.close();
		}
		
	}
}
